﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Practices.EnterpriseLibrary.Data;
using System.Data.Common;
using System.Data;
using SISIN.BE;

namespace SISIN.DA
{
    public class ChatDA
    {
        #region SINGLETON

        private static ChatDA _instance = null;
        /// <summary>
        /// Devuelve una instancia del objeto
        /// </summary>
        /// <remarks></remarks>
        public static ChatDA GetInstance()
        {

            if ((_instance == null))
            {
                _instance = new ChatDA();
            }

            return _instance;

        }

        #endregion

        public Boolean AddMessage(int emisor_id, int receptor_id, String mensaje) {
            Boolean ok = false;

            try {
                Database db = DatabaseFactory.CreateDatabase("ConnectionString");
                DbCommand cmd = db.GetStoredProcCommand("PA_CHAT_INSERT");

                db.AddInParameter(cmd, "@n_usu_ini", DbType.Int32, emisor_id);
                db.AddInParameter(cmd, "@n_usu_fin", DbType.Int32, receptor_id);
                db.AddInParameter(cmd, "@s_cha_mensaje", DbType.String, mensaje);
                db.ExecuteNonQuery(cmd);

                ok = true;
            }
            catch(Exception ex){
                throw;
            }

            return ok;
        }

        public List<ChatBE> GetFromRecepetor(int fuente_id, int receptor_id) {
            List<ChatBE> conversacion = new List<ChatBE>();

            try {
                Database db = DatabaseFactory.CreateDatabase("ConnectionString");
                DbCommand cmd = db.GetStoredProcCommand("PA_CHAT_GET_BY_RECEPTOR");

                db.AddInParameter(cmd, "@usu_fuente", DbType.Int32, fuente_id);
                db.AddInParameter(cmd, "@usu_receptor", DbType.Int32, receptor_id);

                using (IDataReader dr = db.ExecuteReader(cmd))
                {
                    while (dr.Read())
                    {
                        ChatBE obj = new ChatBE();
                        obj.Id = dr.GetInt32(dr.GetOrdinal("n_chat_id"));
                        obj.Emisor.Id = dr.GetInt32(dr.GetOrdinal("n_usuario_ini"));
                        obj.Receptor.Id = dr.GetInt32(dr.GetOrdinal("n_usuario_fin"));
                        obj.Mensaje = dr.GetString(dr.GetOrdinal("s_cha_mensaje"));
                        obj.FechaCreacion = dr.GetDateTime(dr.GetOrdinal("d_cha_fechacreacion"));

                        conversacion.Add(obj);
                    }
                }
            }
            catch (Exception ex) {
                throw;
            }

            return conversacion;
        }

        public int SearchConcurrencias(int fuente_id, int receptor_id, String cadena) {
            int count = 0;
            try
            {
                Database db = DatabaseFactory.CreateDatabase("ConnectionString");
                DbCommand cmd = db.GetStoredProcCommand("PA_CHAT_GET_CONCURRENCIAS");
                
                db.AddInParameter(cmd, "@n_usu_ini", DbType.Int32, fuente_id);
                db.AddInParameter(cmd, "@n_usu_fin", DbType.Int32, receptor_id);
                db.AddInParameter(cmd, "@s_cadena", DbType.String, cadena);

                IDataReader dr = db.ExecuteReader(cmd);

                if (dr.Read())
                {
                    count = Convert.ToInt32(dr["contador"]);
                }
            }
            catch (Exception ex)
            {
                throw;
            }
            return count;
        }
    }
}
